<template>
  <div>
    <el-dialog title="编辑" :visible.sync="dialogVisible" width="400px">
      <el-form class="demo-form-inline" label-width="100px">
        <el-form-item label="地区名称" :required=true>
          <el-input v-model="data.name"/>
      </el-form-item>
      <el-form-item label="地区简称" :required=true v-if="data.level==1">
          <el-input v-model="data.shortName"/>
      </el-form-item>
      <el-form-item label="地区拼音" :required=true>
          <el-input v-model="data.spell"/>
      </el-form-item>
      <el-form-item label="地区编码" :required=true>
          <el-input v-model="data.code"/>
      </el-form-item>
    <el-form-item label="备注">
      <el-input v-model="data.remarks" />
  </el-form-item>
</el-form>

<span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="submit()">提 交</el-button>
</span>
</el-dialog>
</div>
</template>

<script>
    import apiprovince from "@/api/baseManager/province"
    export default {
        name: "province_edit",
        data() {
            return {
                dialogVisible: false,
                data: this.initData()
            };
        },
        methods: {
            initData(){
                return {
                    id:'',
                    level:'', 
                    name: "",
                    shortName: "",
                    spell:"",
                    code:"",
                    population:"",
                    remarks:'',
                    provinceId:'',
                    cityId:''
                }
            },
            open(level, row) {
                this.data = this.initData()
                this.dialogVisible = true;
                if(row.id){ //省市区编辑 逻辑统一
                    apiprovince.get(row.id, level).then(res=>{
                        this.data = res.data.item;
                        this.data.level=level;
                        debugger
                    })
 
                    return
                }

                //新增市
                this.data.level = level;
                if(level==2){
                    this.data.provinceId = row.provinceId
                    return;   
                }
                //新增区
                if(level==3){
                    this.data.provinceId = row.provinceId;
                    this.data.cityId = row.cityId;                    
                    return;
                }
            },
            //提交
            submit(){
                apiprovince.save(this.data).then(res=>{
                    this.dialogVisible = false
                    if(this.data.level==1){
                        this.$parent.getList(); //调父组件方法 刷新表格
                    } else if(this.data.level==2){ //为2时调用父组件的省的下级市
                        this.$parent.loadCity({id:this.data.provinceId}); 
                    } else {
                        this.$parent.loadArea({id:this.data.cityId});
                    }
                });
            }
        }
    };
</script>

<style scoped>
</style>